package com.ctz.commonframe.utils;



import android.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AES {
    /**
     * AES-128-ECB加密
     * @param src 明文
     * @param key 密钥
     * @return 密文
     * @throws Exception
     */
    public static String encrypt(String src, String key) {
        try {
            byte[] raw = key.getBytes("UTF-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"算法/模式/补码方式"
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encrypted = cipher.doFinal(src.getBytes("UTF-8"));
            return new String(Base64.encode(encrypted, Base64.DEFAULT), "UTF-8").trim();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
